using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SyncUdp.UI
{
    using DG.Tweening;
    using SyncUdp.Event.GameEvent;
    using SyncUdp.Interface;
    using UnityEngine.UI;

    public class PlayingUIController : AbstractMonoSingleton, IUIController
    {
        [Header("聊天 UI")]
        [SerializeField, Tooltip("聊天文本")]
        Text _chatTxt;

        [Header("重连 UI")]
        [SerializeField, Tooltip("连接状态面板")]
        GameObject _connectionStatePanel;

        [SerializeField, Tooltip("显示等待的图片")]
        Image _waitImg;
        
        [SerializeField, Tooltip("显示提示的文本")]
        Text _tipTxt;

        [SerializeField, Tooltip("断开连接按钮")]
        Button _disconnectionBtn;

        protected override void InitOnStart()
        {
            RegisterEvent<GameHasDisconnectedEvent>(OnGameHasDisconnected);
            RegisterEvent<GameReconnectedSuccessfullyEvent>(OnGameReconnectedSuccessfully);
            RegisterEvent<OtherPlayerDisconnectedEvent>(OnOtherPlayerDisconnected);
            RegisterEvent<OtherPlayerJoinedGameEvent>(OnOtherPlayerJoinedGame);
            _disconnectionBtn.onClick.AddListener(() => _gm.CloseGame());
        }

        private void ShowMsgOnChatText(string msg)
        {
            _chatTxt.text = $"{System.Environment.NewLine}" + msg;
        }

        IEnumerator ShutDowmTimerCoroutine()
        {
            float time = _gm.CommonData.clientCheckDisconnectionTime;
            while (time >= 0)
            {
                _tipTxt.text = $"尝试重新连接失败，{time} 秒后关闭连接……";
                time--;
                yield return new WaitForSeconds(1);
            }
            _gm.CloseGame();
        }

        #region 方法实现
        public void Show()
        {
            gameObject.SetActive(true);
            _connectionStatePanel.SetActive(false);
            _chatTxt.text = string.Empty;
        }

        public void Hide()
        {
            gameObject.SetActive(false);
        }
        #endregion

        #region 回调方法
        void OnGameHasDisconnected(GameHasDisconnectedEvent e)
        {
            _waitImg.fillAmount = 1;
            if (e.isTryingReconnection)
            {
                _connectionStatePanel.SetActive(true);
                _waitImg.DOFillAmount(0, 1).SetLoops(-1, LoopType.Yoyo);
                _tipTxt.text = "正在尝试重新连接……";
            }
            else
            {
                _waitImg.DOKill();
                StartCoroutine(ShutDowmTimerCoroutine());
            }
        }

        void OnGameReconnectedSuccessfully(GameReconnectedSuccessfullyEvent e)
        {
            _waitImg.fillAmount = 1;
            _connectionStatePanel.SetActive(false);
        }

        void OnOtherPlayerDisconnected(OtherPlayerDisconnectedEvent e)
        {
            ShowMsgOnChatText($"{e.otherPlayerName} 断开连接……");
            _chatTxt.color = Color.white;
            Sequence sq = DOTween.Sequence();
            sq.Kill();
            sq.AppendInterval(2).Append(_chatTxt.DOFade(0, 2));
        }

        void OnOtherPlayerJoinedGame(OtherPlayerJoinedGameEvent e)
        {
            ShowMsgOnChatText($"{e.otherPlayerName} 加入游戏");
            _chatTxt.color = Color.white;
            Sequence sq = DOTween.Sequence();
            sq.Kill();
            sq.AppendInterval(2).Append(_chatTxt.DOFade(0, 2));
        }    
        #endregion
    }
}